<?php

namespace App\Admin\Controllers;

use App\Models\Attachment;
use Encore\Admin\Http\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Table;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Presenters\AttachmentPresenter;

class AttachmentController extends AdminController
{
    protected $title = "附件";


    /**
     * Make a grid builder.
     *
     * @return Table
     */
    protected function table()
    {
        $table = new Table(new Attachment);
        $table->model()->with(['user', 'commentDetails', 'commentHides'])->orderBy('id', 'desc');

        $table->id('Id');
        $table->column('缩略图')->display(function () {
            return imageHtml($this->key, null, 40);
        });
        $table->mime_type('Mime type');
        $table->column('user.name', '上传者');

        $table->key('Key');
        $table->column('大小')->display(function () {
            return $this->humanSize;
        });
        $table->width('Width');
        $table->height('Height');
        $table->shoot_time('Shoot time');
        $table->column('依附于')->display(function () {
            $results = [];
            if ($this->commentDetails->isNotEmpty()) {
                foreach ($this->commentDetails as $commentDetail) {
                    $results[] = sprintf('<a href="%s" target="_blank">%s</a>', route('admin.comment.find', [$commentDetail->cid]), $commentDetail->excerpt);
                }
            }
            if ($this->commentHides->isNotEmpty()) {
                foreach ($this->commentHides as $commentHide) {
                    $results[] = sprintf('<a href="%s" target="_blank">%s</a>', route('admin.comment.find', [$commentHide->cid]), $commentHide->excerpt);
                }
            }
            return implode('、', $results);
        });
        $table->created_at('Created at');

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Attachment::findOrFail($id));

        $show->id('Id');
        $show->uid('Uid');
        $show->mime_type('Mime type');
        $show->key('Key');
        $show->size('Size');
        $show->width('Width');
        $show->height('Height');
        $show->shoot_time('Shoot time');
        $show->latitude('Latitude');
        $show->longitude('Longitude');
        $show->location('Location');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Attachment);

        $form->number('uid', 'Uid');
        $form->text('mime_type', 'Mime type');
        $form->text('key', 'Key');
        $form->number('size', 'Size');
        $form->number('width', 'Width');
        $form->number('height', 'Height');
        $form->datetime('shoot_time', 'Shoot time')->default(date('Y-m-d H:i:s'));
        $form->decimal('latitude', 'Latitude');
        $form->decimal('longitude', 'Longitude');
        $form->text('location', 'Location');

        return $form;
    }
}
